<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

class CI_Digit{
	var $CI;
	public function __construct( ){
		$this->CI =& get_instance();
	}
	
	function generateReferralCode( ){
		$records = $this->CI->db->get(TBL_USER)->result();
		
		$numberArray = array();
		foreach($records as $obj){
			$numberArray[] = $obj->referral_code;
		}
		
		while(true){
			$rand = $this->rand_string(17);
			if(!in_array($rand, $numberArray)){
				return $rand;
			}
		}
	}	
	
	function generateCODcode(){
		$records = $this->CI->db->get(TBL_COD)->result();
		
		$numberArray = array();
		foreach($records as $obj){
			$numberArray[] = $obj->code;
		}
		
		while(true){
			$rand = $this->rand_string(12);
			if(!in_array($rand, $numberArray)){
				return $rand;
			}
		}
	}
	
	function rand_digit($len, $chars = '0123456789')
	{
		$string = '';
		for ($i = 0; $i < $len; $i++)
		{
			$pos = rand(0, strlen($chars)-1);
			$string .= $chars{$pos};
		}
		return $string;
	}
	
	function rand_string($len, $chars = 'abcdefghiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
	{
		$string = '';
		for ($i = 0; $i < $len; $i++)
		{
			$pos = rand(0, strlen($chars)-1);
			$string .= $chars{$pos};
		}
		return $string;
	}
	
	
	//end class
}	